<?php

namespace App\Http\Controllers\Managea;

use App\Traits\BaseFun;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\UsersRepository;


class UsersController extends Controller
{
    public function __construct(
        UsersRepository $UsersRepository

    ) {
        $this->UsersRepository = $UsersRepository;
    }

    public function user(Request $request)
    {
        $where = [];

        if ($request->nickname) {
            $where['nickname'] = $request->nickname;
        }

        if ($request->mobile) {
            $where['mobile'] = $request->mobile;
        }
        if ($request->start_time && $request->end_time) {
            $where['start_time']=$request->start_time.' 00:00:01';
            $where['end_time']=$request->end_time.' 00:00:01';
        }
        $user = $this->UsersRepository->pageList($where);

        if ($user) {
            $ArrDate = ['state' => 0, 'data' => $user, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2, 'data' => '', 'msg' => '获取失败!'];
        }

        return BaseFun::jsonReturn($ArrDate);
    }
    public function userList(Request $request)
    {
       
        $user = $this->UsersRepository->list();

        if ($user) {
            $ArrDate = ['state' => 0, 'data' => $user, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2, 'data' => '', 'msg' => '获取失败!'];
        }

        return BaseFun::jsonReturn($ArrDate);
    }
    public function userState(Request $request, $id)
    {
        $info = $this->UsersRepository->info(['id' => $id]);
        if ($info['state'] == 1) {
            # 封号 删除 token
            $param = ['state' => '2', 'login_token' => '', 'token_time' => ''];
        } elseif ($info['state'] == 2) {
            $param = ['state' => '1'];
        }
        $param['id'] = $id;

        $up = $this->UsersRepository->update($param);
        if ($up) {
            $ArrDate = ['state' => 0, 'data' => $up, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2, 'data' => '', 'msg' => '封禁或解除用户失败'];
        }

        return BaseFun::jsonReturn($ArrDate);
    }

    public function userInfo(Request $request, $id)
    {
        # 用户信息
        $Info = $this->UsersRepository->info(['id' => $id]);

        if ($Info) {
            $ArrDate = ['state' => 0, 'data' => $Info, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2, 'data' => '', 'msg' => '数据获取失败'];
        }

        return BaseFun::jsonReturn($ArrDate);
    }

}
